package com.lindroy.todomvp.tasks

import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.google.android.material.snackbar.Snackbar
import com.lindroy.todomvp.R
import com.lindroy.todomvp.addtask.AddEditTaskActivity
import com.lindroy.todomvp.data.Task
import com.lindroy.todomvp.util.showSnackBar
import kotlinx.android.synthetic.main.activity_tasks.*

/**
 * @author Lin
 * @date 2021/6/25
 * @function 清单列表页面
 * @Description
 */
class TasksFragment : Fragment(), TasksContract.View {

    override lateinit var presenter: TasksContract.Presenter

    //tasksPresenter应该放在TasksActivity中，为了方便，暂时放在这里
    private val tasksPresenter by lazy { TasksPresenter(tasksView = this) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
        }
        tasksPresenter.tasksView
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_tasks, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initOnClick()
    }

    private fun initOnClick() {
        requireActivity().fabAddTask.setOnClickListener {
            presenter.addNewTask()
        }
    }

    companion object {
        @JvmStatic
        fun newInstance() =
            TasksFragment().apply {
                arguments = Bundle().apply {
                }
            }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        presenter.result(requestCode,resultCode)
    }

    override var isActive: Boolean = false
        get() = isAdded


    override fun setLoadingIndicator(active: Boolean) {
    }

    override fun showTasks(tasks: List<Task>) {

    }

    override fun showAddTaskPage() {
        startActivity(Intent(context, AddEditTaskActivity::class.java))
    }

    override fun showTaskDetailsUi(taskId: String) {
    }

    override fun showTaskMarkedComplete() {
    }

    override fun showTaskMarkedActive() {
    }

    override fun showCompletedTasksCleared() {
    }

    override fun showLoadingTasksError() {
    }

    override fun showNoTasks() {
    }

    override fun showActiveFilterLabel() {
    }

    override fun showCompletedFilterLabel() {
    }

    override fun showAllFilterLabel() {
    }

    override fun showNoActiveTasks() {
    }

    override fun showNoCompletedTasks() {
    }

    //显示添加任务成功的提示
    override fun showSuccessfullySavedMessage() {
        showMessage(getString(R.string.successfully_saved_task_message))
    }

    override fun showFilteringPopUpMenu() {
    }

    private fun showMessage(msg:String){
        view?.showSnackBar(msg)
    }
}